$(function () {

    /*选择器抽取的变量*/
    var dictionary_form = $("#dictionary_form");
    var dictionary_dialog = $("#dictionary_dialog");
    var dictionary_datagrid = $("#dictionary_datagrid");
    var dictionaryItem_form = $("#dictionaryItem_form");
    var dictionaryItem_dialog = $("#dictionaryItem_dialog");
    var dictionaryItem_datagrid = $("#dictionaryItem_datagrid");


    /*字典目录数据表*/
    dictionary_datagrid.datagrid({
        url: "/dataDictionary/query.do",
        fit: true,
        striped: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: "#dictionary_toolbar",
        /*点击行事件*/
        onSelect: function (index, row) {
            //重新加载明细表
            dictionaryItem_datagrid.datagrid("load",{sn:row.sn})
        },
        columns: [[
            {field: 'sn', title: '目录编号', width: 180, align: 'center'},
            {field: 'name', title: '目录名称', width: 180, align: 'center'},
            {field: 'info', title: '目录简介', width: 180, align: 'center'}
        ]]
    })

    /*明细目录数据表*/
    dictionaryItem_datagrid.datagrid({
        url: "/dataDictionaryItem/query.do",
        fit: true,
        striped: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        toolbar: "#dictionaryItem_toolbar",
        columns: [[
            {field: 'name', title: '明细名称', width: 180, align: 'center'},
            {field: 'info', title: '明细简介', width: 180, align: 'center'},
            {field: 'dictionary', title: '所属目录', width: 180, align: 'center',formatter:function (value,row,index) {
                    return value?value.name:'';
                }}
        ]]
    })

    /*目录编辑框*/
    dictionary_dialog.dialog({
        width: 280,
        height: 200,
        buttons: "#dictionary_btns",
        closed: true,
        onClose: function () {
            dictionary_form.form("clear");
        }
    });

    /*明细编辑框*/
    dictionaryItem_dialog.dialog({
        width: 280,
        height: 200,
        buttons: "#dictionaryItem_btns",
        closed: true,
        onClose: function () {
            dictionaryItem_form.form("clear");
        }

    });

    /*事件对象*/
    var methodObject = {
        /*添加事件*/
        add: function () {
            dictionary_dialog.dialog("setTitle", "添加");
            dictionary_dialog.dialog("open");
        },
        /*编辑事件*/
        edit: function () {
            var row = dictionary_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择一行数据', 'info');
                return;
            }
            dictionary_dialog.dialog("setTitle", "编辑");
            /*回显*/
            dictionary_form.form("load", row);
            dictionary_dialog.dialog("open");
        },
        save: function () {
            dictionary_form.form("submit", {
                url: "/dataDictionary/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            dictionary_dialog.dialog("close");
                            dictionary_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "error");
                    }
                }
            })
        },
        remove: function () {

            var row = dictionary_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "info");
                return;
            }

            $.messager.confirm("温馨提示", "确定要删除吗?", function (r) {
                if (r) {
                    $.get("/dataDictionary/delete.do?id=" + row.id, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","操作成功", "info", function () {
                                dictionary_datagrid.datagrid("reload");
                                dictionaryItem_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.message,"error");
                        }
                    })
                }
            })
        },

        cancel: function () {
            dictionary_dialog.dialog("close");
        },
        
        reload: function () {
            dictionary_datagrid.datagrid("reload");
        },
        
        /*明细的事件,和目录一模一样*/
        addItem: function () {
            dictionaryItem_dialog.dialog("setTitle", "添加");
            dictionaryItem_dialog.dialog("open");
        },
        editItem: function () {

            var row = dictionaryItem_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('温馨提示', '请选择一行数据', 'info');
                return;
            }

            dictionaryItem_form.form("clear");
            dictionaryItem_dialog.dialog("setTitle", "编辑");

            if(row.dictionary){
                row["dictionary.id"] = row.dictionary.id;
            }

            dictionaryItem_form.form("load", row);

            dictionaryItem_dialog.dialog("open");
        },
        saveItem: function () {
            dictionaryItem_form.form("submit", {
                url: "/dataDictionaryItem/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", "操作成功", "info", function () {
                            dictionaryItem_dialog.dialog("close");
                            dictionaryItem_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "error");
                    }
                }
            })
        },
        removeItem: function () {
            var row = dictionaryItem_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert("温馨提示", "请选择一行数据", "info");
                return;
            }

            $.messager.confirm("温馨提示", "确定要删除吗?", function (r) {
                if (r) {
                    //鑾峰彇閫変腑琛岀殑id
                    $.get("/dataDictionaryItem/delete.do?id=" + row.id, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info", function () {
                                dictionaryItem_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.message,"error");
                        }
                    })
                }
            })
        },
        cancelItem: function () {
            dictionaryItem_dialog.dialog("close");
        },
        reloadItem: function () {
            dictionaryItem_datagrid.datagrid("reload");
        }
    };

    /*统一事件分配*/
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObject[cmd]();
    });
})
